package com.yunbiao.ybbusstopplate.serialport.agreement;

import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.FileIOUtils;
import com.blankj.utilcode.util.LogUtils;
import com.yunbiao.ybbusstopplate.bean.SiteInfoBean;
import com.yunbiao.ybbusstopplate.ui.fragment.BusStopPlateFragment2;
import com.yunbiao.ybbusstopplate.utils.BytesUtils;
import com.yunbiao.ybbusstopplate.utils.StringUtils;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class TD2020PortAgreement extends BaseAgreement {
    private static final String TAG = "TD2020PortAgreement";

    private static final DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

    private static TD2020PortAgreement td2020PortAgreement = null;

    private TD2020PortAgreement(){}

    public static TD2020PortAgreement getInstance(){
        if (td2020PortAgreement == null) {
            td2020PortAgreement = new TD2020PortAgreement();
        }
        return td2020PortAgreement;
    }

    private StringBuilder hexString = null;
    private String device = "";

    /**
     * 串口数据解析
     *
     * @param bytes
     */
    public synchronized void analysisData(byte[] bytes) {
        if (bytes == null) {
            Log.d(TAG,"bytes is null");
            return;
        }
        if (hexString == null) {
            hexString = new StringBuilder();
        }
        //起始字节
        byte[] start = BytesUtils.SubByte(bytes, 0, 2);
        String startHex = BytesUtils.bytesToHex(start);

        //485转TTL有时候会出现结尾5500的情况，需要把结尾的00去掉之后再校验
        byte[] end1 = BytesUtils.SubByte(bytes,bytes.length - 2,2);
        String endHex2 = BytesUtils.bytesToHex(end1);
//        Log.d(TAG, "analysisData: 结尾：" + endHex2);
        if(TextUtils.equals("5500",endHex2)){
//            Log.d(TAG, "analysisData: 结尾是5500，截取后解析");
            bytes = BytesUtils.SubByte(bytes,0,bytes.length - 1);
        }

        //末尾字节
        byte end = bytes[bytes.length - 1];
        String endHex = BytesUtils.byteToHex(end);

        //开始
        if ("bb10".equals(startHex)) {
//            Log.d(TAG, "-----------start---------");
            hexString.delete(0, hexString.length());
            device = BytesUtils.byteToHex(bytes[1]);
        }
        //存储数据
        hexString.append(BytesUtils.bytesToHex(bytes));
        //结束
        if ("55".equals(endHex)) {
//            Log.d(TAG, "-----------end---------");
//            Log.d(TAG, "--->" + hexString.toString());
            String splitStr = "bb" + device;
            String[] hexArray = hexString.toString().split(splitStr);
            hexString.setLength(0);

            for (String ss : hexArray) {
                if (ss != null && !"".equals(ss)) {
                    byte[] byteArray = BytesUtils.hexToByteArray(splitStr + ss);
                    chioseAgr(byteArray);
                }
            }
        }
    }

    private void chioseAgr(byte[] bytes) {
        try {
//            String s = BytesUtils.bytesToHex(bytes);
//            Log.d(TAG, "chioseAgr: " + s);

            byte ADDR = bytes[1];
            byte CMD = bytes[3];
            byte[] data = BytesUtils.SubByte(bytes, 4, bytes.length - 7);

            if(ADDR != (byte)0X10){
                return;
            }

            //判断是否是时间
            if(CMD == (byte)0x11 && data.length == 7){
                try {
                    String string = BytesUtils.bytesToHex(data);
                    Date parse = sdf.parse(string);
                    d("是时间：" + string);
                    return;
                } catch (ParseException e) {
                    Log.e(TAG, "chioseAgr: ", e);
                }
            }

            Queue<Byte> byteQueue = new LinkedList<>();
            for (byte datum : data) {
                byteQueue.add(datum);
            }

            switch (CMD) {
                case (byte)0x02://路牌字符（线路信息）
                    resolveGuideBoardWord(byteQueue);
                    break;
                case (byte)0x08://报站
                    broadSite(byteQueue);
                    break;
                case (byte)0x11://侧牌站点（站点列表）
                    siteList(byteQueue);
                    break;
                case (byte)0x12://首末班时间
                    firstLastTime(byteQueue);
                    break;
            }
        }  catch (Exception e) {
            e("解析异常：" + e);
//            if (agreementListener != null) {
//                agreementListener.agreementFailed();
//            }
        }
    }

    private String startSite,endSite;
    //路牌字符
    private void resolveGuideBoardWord(Queue<Byte> byteQueue){
        // BB 10 1F 02 0B B2 DC D6 BE BA C0 37 30 37 CF DF 04 BB AA CF C4 08 BB A8 C7 C5 C9 E7 C7 F8 86 10 55
        // BB 起始
        // 10 硬件地址
        // 1F 长度
        // 02 命令字
        // 0B 线路号长度
        // B2 DC D6 BE BA C0 37 30 37 CF DF 线路号字符
        // 04 起点长度
        // BB AA CF C4 起点字符
        // 08 终点长度
        // BB A8 C7 C5 C9 E7 C7 F8 终点字符
        // 86 SUML
        // 10 SUMH
        // 55 结尾
        int length = BytesUtils.hex16to10(BytesUtils.byteToHex(byteQueue.poll()));
        String lineNumber = StringUtils.hexStringToString(BytesUtils.bytesToHex(getBytes(byteQueue,length)));

        length = BytesUtils.hex16to10(BytesUtils.byteToHex(byteQueue.poll()));
        startSite = StringUtils.hexStringToString(BytesUtils.bytesToHex(getBytes(byteQueue,length)));

        length = BytesUtils.hex16to10(BytesUtils.byteToHex(byteQueue.poll()));
        endSite = StringUtils.hexStringToString(BytesUtils.bytesToHex(getBytes(byteQueue,length)));

        d("线路号：" + lineNumber + " , " + "起点字符：" + startSite + " , " + "终点字符：" + endSite);

        if(agreementListener != null){
            agreementListener.onLineInfoResult(new String[]{lineNumber,startSite,endSite});
        }
    }

    //报站
    private void broadSite(Queue<Byte> byteQueue){
        // BB 10 18 08 80 8A 20 C6 F0 B5 E3 D5 BE 20 BB A8 C7 C5 C9 E7 C7 F8 20 D9 0C 55
        // BB 起始
        // 10 硬件地址
        // 18 长度
        // 08 命令字
        // 80 站点号 [10000000] [7]1=到站0=离站，[0-6]站点序号
        // 8A 上下行 [10001010] [7]0=上行1=下行，[0-6]站点总数
        // 20 C6 F0 B5 E3 D5 BE 20 BB A8 C7 C5 C9 E7 C7 F8 20 报站内容
        // D9 SUML
        // 0C SUMH
        // 55 结尾
        byte SITE_INDEX_IN_OUT = byteQueue.poll();
        StringBuilder siteIndexInOutBits = new StringBuilder(ConvertUtils.bytes2Bits(new byte[]{SITE_INDEX_IN_OUT}));
        //进出站
        char inOut = siteIndexInOutBits.charAt(0);
        //站点列表
        siteIndexInOutBits.setCharAt(0,'0');
        int siteIndex = BytesUtils.hex16to10(BytesUtils.byteToHex(BitToByte(siteIndexInOutBits.toString())));

        byte SITE_COUNT_UP_DOWN = byteQueue.poll();
        StringBuilder siteCountUpDownBits = new StringBuilder(ConvertUtils.bytes2Bits(new byte[]{SITE_COUNT_UP_DOWN}));
        //上下行
        char upDown = siteCountUpDownBits.charAt(0);
        siteCountUpDownBits.setCharAt(0,'0');
        int siteCount = BytesUtils.hex16to10(BytesUtils.byteToHex(BitToByte(siteCountUpDownBits.toString())));
        d("站点序号：" + siteIndex + " --- 进出站：" + inOut + " --- 上下行：" + upDown + " --- 站点总数：" + siteCount);

        String broadSiteContent = StringUtils.hexStringToString(BytesUtils.bytesToHex(getBytes(byteQueue,byteQueue.size())));

        //增加报站控制，
        boolean isSame = true;
        String s = "";
        String s2 = "";
        if (!TextUtils.isEmpty(broadSiteContent)) {
            if(siteList != null && !siteList.isEmpty()){
                int i = inOut == '1' ? siteIndex : siteIndex + 1;
                s2 = siteList.get(i);
                isSame = broadSiteContent.contains(s2);
                if(isSame){
                    s = "报站内容一致";
                } else {
                    s = "报站内容不一致：" + s2;
                }
            }
        }
        //报站内容不一致则不播报
        if(isSame){
            d("报站内容：" + broadSiteContent + " --- " + s);
        } else {
            e("报站内容：" + broadSiteContent + " --- " + s);
        }

        if (agreementListener != null) {
            SiteInfoBean siteInfoBean = new SiteInfoBean();
            siteInfoBean.setIndex(-1);
            siteInfoBean.setStutas(inOut == '1' ? 1 : 0);
            siteInfoBean.setShowTime(10);
            siteInfoBean.setSiteName(s2);
            agreementListener.onSiteTipResult(siteInfoBean);

//            ArrayList<String> list = BusStopPlateFragment2.Companion.getList();
//            int index1 = siteInfoBean.getIndex();
//            String siteName="";
//            if (index1 < 0){
//                siteName = siteInfoBean.getSiteName();
//            }else{
//                if (index1>=list.size()){
//                    index1=list.size()-1;
//                }
//                siteName=list.get(index1);
//            }
//
//            int stutas = siteInfoBean.getStutas();
//            if(broadSiteContent.contains(siteName)){
//                Log.d(TAG, "broadSite: 屏幕报站一致：" + siteName + "," + (stutas == 1 ? "到站" : "下一站") + " : " + broadSiteContent);
//            } else {
//                Log.e(TAG, "broadSite: 屏幕报站不一致：" + siteName + "," + (stutas == 1 ? "到站" : "下一站") + " : " + broadSiteContent);
//            }
        }
    }

    private List<String> siteList;

    private boolean isRefreshList = false;

    //线路站点
    private void siteList(Queue<Byte> byteQueue) {
        // BB 10 50 11 08 00 04 BB AA CF C4 01 08 BF AA B8 A3 CB C2 B6 AB 02 06 D6 F1 C9 BD D4 B0 03 0A CF E6 D1 C5 C2 B7 CE F7 BF DA 04 06 B3 B1 D7 DA BD D6 05 08 BB F0 D1 E6 C2 B7 BF DA 06 08 D7 D8 CA F7 C2 B7 BF DA 07 08 BB A8 C7 C5 C9 E7 C7 F8 68 2E 55
        // BB 起始
        // 10 硬件地址
        // 50 长度
        // 11 命令字
        // 08 站点总数
        // 00 站点序号
        // 04 站点名长度
        // BB AA CF C4 站点名
        // 01
        // 08
        // BF AA B8 A3 CB C2 B6 AB
        // 02
        // 06
        // D6 F1 C9 BD D4 B0
        // 03
        // 0A
        // CF E6 D1 C5 C2 B7 CE F7 BF DA
        // 04
        // 06
        // B3 B1 D7 DA BD D6
        // 05
        // 08
        // BB F0 D1 E6 C2 B7 BF DA
        // 06
        // 08
        // D7 D8 CA F7 C2 B7 BF DA
        // 07
        // 08
        // BB A8 C7 C5 C9 E7 C7 F8
        // 68
        // 2E
        // 55

        if(siteList == null){
            siteList = new LinkedList<>();
        }

        int siteCount = BytesUtils.hex16to10(BytesUtils.byteToHex(byteQueue.poll()));
//        d("站点总数：" + siteCount);

        int tag = 0;
        int length = 0;
        int siteIndex = 0;
        readList:while (!byteQueue.isEmpty()) {
            switch (tag) {
                case 0:
                    siteIndex = BytesUtils.hex16to10(BytesUtils.byteToHex(byteQueue.poll()));
//                    d("站点序号：" + siteIndex);
                    if(siteIndex == 0){
                        d("发现起始站，清除旧数据");
                        siteList.clear();
                        isRefreshList = true;
                    }
                    tag = 1;
                    break;
                case 1:
                    length = BytesUtils.hex16to10(BytesUtils.byteToHex(byteQueue.poll()));
//                    d("站点名长度：" + length);
                    tag = 2;
                    break;
                case 2:
                    String siteName = StringUtils.hexStringToString(BytesUtils.bytesToHex(getBytes(byteQueue,length)));
                    d("站点名：" + siteIndex + "_" + siteName + "，站点总数：" + siteCount + " --- " + (isRefreshList && siteList.size() < siteCount));
                    //增加站名收集控制，如果不在刷新状态则不添加
                    if(isRefreshList && siteList.size() < siteCount){
                        siteList.add(siteName);
                        if(siteList.size() >= siteCount){
                            Log.d(TAG, "handleSiteList: ------------------------------ ");
                            break readList;
                        }
                    }
                    tag = 0;
                    break;
            }
        }

        //增加控制，如果不在刷新状态则不添加刷新站点列表
        if(isRefreshList && siteList.size() >= siteCount){
            isRefreshList = false;
            if(siteCount != siteList.size()){
                e("获取站点列表完成，通知界面刷新：" + siteCount + " , " + siteList.size() + " , " + siteList);
                e("站点列表数量错误");
            } else {
                d("获取站点列表完成，通知界面刷新：" + siteCount + " , " + siteList.size() + " , " + siteList);
                d("站点列表数量正确");
            }
            String s = siteList.get(0);
            String s1 = siteList.get(siteList.size() - 1);
            if(TextUtils.equals(s,startSite) && TextUtils.equals(s1,endSite)){
                d("站点列表正确");
            } else {
                e("站点列表异常");
            }
            if(agreementListener != null){
                agreementListener.onSiteListResult(new ArrayList<>(siteList));
            }
        }
    }

    //首末班时间
    private void firstLastTime(Queue<Byte> byteQueue){
        // bb 10 09 12 06 00 16 1e 00 65 55
        // bb 起始
        // 10 硬件地址
        // 09 长度
        // 12 命令字
        // 06 00 首班
        // 16 1e 末班
        // 00 SUML
        // 65 SUMH
        // 55 结尾
        int startHourInt = BytesUtils.hex16to10(BytesUtils.byteToHex(byteQueue.poll()));
        String startHour = startHourInt < 10 ? ("0" + startHourInt) : ( "" + startHourInt);
        int startMinutesInt = BytesUtils.hex16to10(BytesUtils.byteToHex(byteQueue.poll()));
        String startMinutes = startMinutesInt < 10 ? ("0" + startMinutesInt) : ( "" + startMinutesInt);
        String startTime = startHour + ":" + startMinutes;
        d("开始时间：" + startTime);
        int endHourInt = BytesUtils.hex16to10(BytesUtils.byteToHex(byteQueue.poll()));
        String endHour = endHourInt < 10 ? ("0" + endHourInt) : ( "" + endHourInt);
        int endMinutesInt = BytesUtils.hex16to10(BytesUtils.byteToHex(byteQueue.poll()));
        String endMinutes = endMinutesInt < 10 ? ("0" + endMinutesInt) : ( "" + endMinutesInt);
        String endTime = endHour + ":" + endMinutes;
        d("结束时间：" + endTime);

        if (agreementListener!=null){
            agreementListener.onSiteTimeResult(startTime,endTime,"","");
        }
    }

    private byte[] getBytes(Queue<Byte> byteQueue,int length){
        byte[] bytes = new byte[length];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = byteQueue.poll();
        }
        return bytes;
    }
    /**
     * Bit转Byte
     */
    public static byte BitToByte(String byteStr) {
        int re, len;
        if (null == byteStr) {
            return 0;
        }
        len = byteStr.length();
        if (len != 4 && len != 8) {
            return 0;
        }
        if (len == 8) {// 8 bit处理
            if (byteStr.charAt(0) == '0') {// 正数
                re = Integer.parseInt(byteStr, 2);
            } else {// 负数
                re = Integer.parseInt(byteStr, 2) - 256;
            }
        } else {//4 bit处理
            re = Integer.parseInt(byteStr, 2);
        }
        return (byte) re;
    }

    private void d(String log){
        Log.d(TAG, log);
//        LogUtils.dTag(TAG,log);
    }

    private void e(String error){
        Log.e(TAG, error);
//        LogUtils.eTag(TAG,error);
    }

    private static final String TAG1 = "TD2020";

    private List<String> dataStrings = new ArrayList<>();
    public void test1(){
        if(dataStrings.isEmpty()){
            new Thread(() -> {
                File file = new File(Environment.getExternalStorageDirectory(),"456456.txt");
                List<String> strings = FileIOUtils.readFile2List(file);

                for (String string : strings) {
                    if(!string.contains("<---")){
                        continue;
                    }
                    String s = string.split("<---")[1];
                    dataStrings.add(s);
                }
                Log.d(TAG, "test1: 添加数据：" + dataStrings.size());
            }).start();
        } else {
            Iterator<String> iterator = dataStrings.iterator();
            while (iterator.hasNext()) {
                String next = iterator.next();
                iterator.remove();
                Log.d(TAG, "test1: " + next);

                byte[] bytes = BytesUtils.hexToByteArray(next);
                analysisData(bytes);

                if(next.endsWith("55")){
                    break;
                }
            }
        }
//        Disposable subscribe = Observable.create(new ObservableOnSubscribe<byte[]>() {
//                    @Override
//                    public void subscribe(ObservableEmitter<byte[]> emitter) throws Exception {
////                        File file = new File(Environment.getExternalStorageDirectory(), "243242.txt");
//                        File file = new File(Environment.getExternalStorageDirectory(), "123.txt");
//                        List<String> strings = FileIOUtils.readFile2List(file);
//                        for (String string : strings) {
//                            if (!string.contains("<---")) {
//                                if(string.contains("打开串口")){
//                                    Log.e(TAG, "subscribe: 打开串口" + " ---------------------------------------------------------------------- ");
//                                } else if(string.contains("关闭串口")){
//                                    Log.e(TAG, "subscribe: 关闭串口" + " ---------------------------------------------------------------------- ");
//                                }
//                                continue;
//                            }
//                            String[] split = string.split("<---");
//                            String hex = split[1];
//                            byte[] bytes = BytesUtils.hexToByteArray(hex);
////                            Log.d(TAG, "test1: --->" + hex);
//
////                            if(!string.contains("len:")){
////                                continue;
////                            }
////                            Log.d(TAG, "subscribe: " + string);
////                            String[] split = string.split("len:");
////                            String s = split[1];
////                            Log.d(TAG, "subscribe: " + s);
////                            String[] split1 = s.split(":");
////                            String hex = split1[1].replaceAll(" ", "");
////                            Log.d(TAG, "subscribe: " + hex);
////                            byte[] bytes = BytesUtils.hexToByteArray(hex);
//
//                            try {
//                                analysisData(bytes);
//                                /*byte[] bytes1 = BytesUtils.SubByte(bytes, 0, 2);
//                                byte aByte = bytes[3];
//                                String start = BytesUtils.bytesToHex(bytes1);
//                                if(TextUtils.equals("bb10",start)){
////                            Log.d(TAG, "subscribe: " + BytesUtils.byteToHex(aByte));
//                                    switch (aByte) {
////                                    case (byte)0x02:
////                                        Log.e(TAG1, "线路信息 ：" + hex);
////                                        emitter.onNext(bytes);
////                                        break;
//                                        case (byte)0x11:
//                                            Log.e(TAG1, "站点列表 ：" + hex);
//                                            analysisData(bytes);
////                                        emitter.onNext(bytes);
//                                            break;
////                                    case (byte) 0x08:
////                                        Log.e(TAG1, "报站 ：" + hex);
////                                        emitter.onNext(bytes);
////                                        break;
//                                        case (byte) 0x01:
//                                        case (byte) 0x03:
//                                        case (byte) 0x10:
//                                        case (byte) 0x12:
//                                            break;
//                                        default:
//                                            Log.e(TAG1, hex);
////                                    Log.d(TAG, "test1: 站点列表 ：" + hex);
//                                            analysisData(bytes);
////                                        emitter.onNext(bytes);
//                                            break;
////                                case (byte)0x12:
////                                    Log.d(TAG, "test1: 首末班时间 ：" + hex);
////                                    break;
//                                    }
//                                } else {
//                                    analysisData(bytes);
//                                }*/
//
////                                Thread.sleep(2000);
//
//                            } catch (Exception e){
//                                Log.e(TAG, "subscribe: ", e);
//                            }
//
//                        }
//
//                        emitter.onComplete();
//                    }
//                }).subscribeOn(Schedulers.single())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Consumer<byte[]>() {
//                    @Override
//                    public void accept(byte[] bytes) throws Exception {
//                        Log.d(TAG, "accept: ------------------ " + BytesUtils.bytesToHex(bytes));
//
//                    }
//                });
//        String[] strings = {
//                "bb10d6112e000ab0b2c6bdc7c5b9abd4b00110bdf0c9b3b3c7a3a8b4f3c8f3b7a2a3a90208baa3b0cbc2b7bfda0308b0b2baa3b3b5d5be0406c9cfd4c3b3",
//                "c70506c1f9bdc7cda40604c6d6b1df0708bae3b0b2bcafcdc5080acef7eeb428b1fbd8c8290904bfc9c4bd0a08c8fdd2e6b8d6ccfa0b08b0b2cfe9c2b7bf",
//                "da0c0ac1e9cbaebed3ceafbbe10d08cbb9c0bcb7fec3b30e0ac1e9d4b4c5c9b3f6cbf90f04c1d6bfda1004c4daccc11108c9e7b5eac2b7bfda1208c7b0d1",
//                "d8c9e7c7f81304b8a3c6d21408d3b5befcc2b7bfda1504b0acc5c9cd8055",
//                "bb10d2112e160cbdfabdadc0cfc4eab4f3d1a71704c6d5d5d5180cbdfabdadb3a4cdbeb3b5d5be1908bdfabdadbec6b3a71a0abdfabdadb4f3bee7d4ba1b",
//                "0abdfabdadc6fbb3b5d5be1c0abdfabdadd6d0d2bdd4ba1d08b1a6c1fabec6b5ea1e08cbabb9b5c7c5cdb71f0ac1fabafebccecceccfc22008d3adb1f6b9",
//                "e3b3a12104b3d8b5ea2204e4b4ccb62304b6abc9bd2408b0b2cca4d0acd2b52504bbaad6de2608baa3cbbfbeb0b3c72706d0cbcfbcc2b72806b6e4c1abcb",
//                "c2290ad2e5c8abbdd6cef7b6ce2a08d0d2b8a3bdd6bfdaad8155",
//                "bb1020112e2b08baa3b9d8b4f3c2a52c06b9d8b5dbc3ed2d06cec4bbafb9ac351055",
//                "bb100c1120230316101017c00055",
//                "bb100c1120230316101028d10055",
//                "bb100c1120230316101040e90055"
//        };
//
//        String[] ss = {
//                "bb10de112f0006cec4bbafb9ac0106b9d8b5dbc3ed0208baa3b9d8b4f3c2a50308d0d2b8a3bdd6bfda040ad2e5c8abbdd6cef7b6ce0506b6e4c1abcbc206",
//                "06d0cbcfbcc2b70708baa3cbbfbeb0b3c70804bbaad6de0908b0b2cca4d0acd2b50a04b6abc9bd0b04e4b4ccb60c04b3d8b5ea0d04d0c2b5ea0e08d3adb1",
//                "f6b9e3b3a10f0ac1fabafebccecceccfc21008cbabb9b5c7c5cdb71108b1a6c1fabec6b5ea120abdfabdadd6d0d2bdd4ba130abdfabdadc6fbb3b5d5be14",
//                "0abdfabdadb4f3bee7d4ba1508bdfabdadbec6b3a7160cbdfabdadb3a4cdbeb3b5d5bebc8555",
//                "bb10de112f1704c6d5d5d5180cbdfabdadc0cfc4eab4f3d1a71904b0acc5c91a08d3b5befcc2b7bfda1b04b8a3c6d21c08c7b0d1d8c9e7c7f81d08c9e7b5",
//                "eac2b7bfda1e04c4daccc11f04c1d6bfda200ac1e9d4b4c5c9b3f6cbf92108cbb9c0bcb7fec3b3220ac1e9cbaebed3ceafbbe12308b0b2cfe9c2b7bfda24",
//                "08c8fdd2e6b8d6ccfa2504bfc9c4bd260acef7eeb428b1fbd8c8292708bae3b0b2bcafcdc52804c6d6b1df2906c1f9bdc7cda42a06c9cfd4c3b3c72b08b0",
//                "b2baa3b3b5d5be2c08baa3b0cbc2b7bfda2d10bdf0c9b3b3c7a3a8b4f3c8f3b7a2a3a96c8855",
//                "bb1012112f2e0ab0b2c6bdc7c5b9abd4b0f30755",
//                "bb100c1120230316101149f30055",
//                "bb100c1120230316101308b40055",
//                "bb100c1120230316101310bc0055"
//        };

//        String[] ss = {
//                "bb10d6112e000ab0b2c6bdc7c5b9abd4b00110bdf0c9b3b3c7a3a8b4f3c8f3b7a2a3a90208baa3b0cbc2b7bfda0308b0b2baa3b3b5d5be0406c9cfd4c3b3",
//                "c70506c1f9bdc7cda40604c6d6b1df0708bae3b0b2bcafcdc5080acef7eeb428b1fbd8c8290904bfc9c4bd0a08c8fdd2e6b8d6ccfa0b08b0b2cfe9c2b7bf",
//                "da0c0ac1e9cbaebed3ceafbbe10d08cbb9c0bcb7fec3b30e0ac1e9d4b4c5c9b3f6cbf90f04c1d6bfda1004c4daccc11108c9e7b5eac2b7bfda1208c7b0d1",
//                "d8c9e7c7f81304b8a3c6d21408d3b5befcc2b7bfda1504b0acc5c9cd8055",
//                "bb10d2112e160cbdfabdadc0cfc4eab4f3d1a71704c6d5d5d5180cbdfabdadb3a4cdbeb3b5d5be1908bdfabdadbec6b3a71a0abdfabdadb4f3bee7d4ba1b",
//                "0abdfabdadc6fbb3b5d5be1c0abdfabdadd6d0d2bdd4ba1d08b1a6c1fabec6b5ea1e08cbabb9b5c7c5cdb71f0ac1fabafebccecceccfc22008d3adb1f6b9",
//                "e3b3a12104b3d8b5ea2204e4b4ccb62304b6abc9bd2408b0b2cca4d0acd2b52504bbaad6de2608baa3cbbfbeb0b3c72706d0cbcfbcc2b72806b6e4c1abcb",
//                "c2290ad2e5c8abbdd6cef7b6ce2a08d0d2b8a3bdd6bfdaad8155",
//                "bb1020112e2b08baa3b9d8b4f3c2a52c06b9d8b5dbc3ed2d06cec4bbafb9ac351055"
//        };
//
//        for (String string : ss) {
//            byte[] bytes = BytesUtils.hexToByteArray(string);
//            analysisData(bytes);
//        }


        //路牌字符
//        String s = "BB 10 1F 02 0B B2 DC D6 BE BA C0 37 30 37 CF DF 04 BB AA CF C4 08 BB A8 C7 C5 C9 E7 C7 F8 86 10 55".replaceAll(" ","").trim();
//        analysisData(BytesUtils.hexToByteArray(queue.poll()));
    }

    public void test2(){
        String
        s = "a1fa5d";
        String s1 = StringUtils.hexStringToString(s);
        Log.d(TAG, "test2: " + s + " --> " + s1);

        s = "5ba3cba3b6a3b0a3b8";
        s1 = StringUtils.hexStringToString(s);
        Log.d(TAG, "test2: " + s + " --> " + s1);

        s = "b0b2c6bdc7c5b9abd4b0";
        s1 = StringUtils.hexStringToString(s);
        Log.d(TAG, "test2: " + s + " --> " + s1);
        //线路站点
//        String s = "BB 10 50 11 08 00 04 BB AA CF C4 01 08 BF AA B8 A3 CB C2 B6 AB 02 06 D6 F1 C9 BD D4 B0 03 0A CF E6 D1 C5 C2 B7 CE F7 BF DA 04 06 B3 B1 D7 DA BD D6 05 08 BB F0 D1 E6 C2 B7 BF DA 06 08 D7 D8 CA F7 C2 B7 BF DA 07 08 BB A8 C7 C5 C9 E7 C7 F8 68 2E 55".replaceAll(" ","").trim();
//        analysisData(BytesUtils.hexToByteArray(s));
    }

    public void test3(){
        //首末班时间
        String s = "BB 10 09 12 06 00 16 1E 00 65 55".replaceAll(" ","").trim();
        analysisData(BytesUtils.hexToByteArray(s));
    }
    public void test4(){
        //报站
        String s = "BB 10 18 08 80 8A 20 C6 F0 B5 E3 D5 BE 20 BB A8 C7 C5 C9 E7 C7 F8 20 D9 0C 55".replaceAll(" ","").trim();
        analysisData(BytesUtils.hexToByteArray(s));


        // 1000 1010 下行，下行总站点数10
        // 0000 1000上行，上行总站点数10
    }

    // bb 起始
    // 10 硬件地址
    // 10 数据长度
    // 02 命令字：路牌字符
    // 00 线路名长度
    // 08 起点站长度
    // bbd8b3a1b3e4b5e7 起点站字符
    // 00 终点站长度
    // 440655 校验位终止字符



    // bb 起始
    // 10 硬件地址
    // 1e 数据长度
    // 02 命令字：路牌字符
    // 03 线路名长度
    // a1fa5d 线路名字符
    // 09 起点站长度
    // 5ba3cba3b6a3b0a3b8 起点站字符
    // 0a 终点站长度
    // b0b2c6bdc7c5b9abd4b0 终点站字符
    // 670f55
}
